<script setup>
import {getCurrentInstance, inject, ref} from "vue";
import {ElMessage} from "element-plus";
import {Plus} from '@element-plus/icons-vue';

const worker_name = ref("")
const worker_salary = ref("")
const worker_money = ref("")
const visible = ref(false);
const {proxy} = getCurrentInstance();
const imageUrl = ref(null);
const formData = new FormData();
const dialogData = inject('modifyDialog');
const emits = defineEmits(['close']);

const open = () => {
  worker_name.value = dialogData.value[0];
  worker_salary.value = dialogData.value[1];
  worker_money.value = dialogData.value[2];
  if (dialogData.value[3] !== null) imageUrl.value = "http://localhost:8091/" + dialogData.value[3];
  visible.value = true;
}

const close = () => {
  visible.value = false
  worker_name.value = "";
  worker_salary.value = "";
  worker_money.value = "";
  imageUrl.value = "";
  emits('close', false);
}

defineExpose({
  open
})

const submitWorker = () => {
  formData.append("id",dialogData.value[4]);
  formData.append("workerName", worker_name.value);
  formData.append("workerSalary", worker_salary.value);
  formData.append("workerMoney", worker_money.value);
  proxy.$axios.post("http://localhost:8091/TbWorker/saveWorker", formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  }).then(resp => {
    if (resp.data > 0) {
      ElMessage.success("操作成功");
    } else {
      ElMessage.error("操作失败");
    }
    visible.value = false;
    worker_name.value = "";
    worker_salary.value = "";
    worker_money.value = "";
  })
}

const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!');
    return false;
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!');
    return false;
  }
  imageUrl.value = URL.createObjectURL(rawFile);
  formData.append('multipartFile', rawFile)
  return true;
};
</script>

<template>
  <el-dialog v-model="visible" @close="close" width="20%">
    <div id="formStyle">
      <el-form>
        <el-form-item><span>姓名</span>
          <el-input v-model="worker_name"></el-input>
        </el-form-item>
        <el-form-item><span>工资薪水</span>
          <el-input v-model="worker_salary"></el-input>
        </el-form-item>
        <el-form-item><span>元/桶</span>
          <el-input v-model="worker_money"></el-input>
        </el-form-item>
        <el-form-item>
          <span>照片</span>
          <el-upload
              action=""
              :show-file-list="false"
              :before-upload="beforeAvatarUpload"
              class="avatar-uploader"
          >
            <img v-if="imageUrl" :src="imageUrl" alt="" class="avatar"/>
            <el-icon v-else class="el-icon-plus avatar-uploader-icon">
              <Plus/>
            </el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitWorker">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<style scoped lang="scss">
.el-dialog {
  .el-input {
    margin-left: 20px;
    width: 70%;
  }

  #formStyle {
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .avatar-uploader {
    margin-left: 20px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

    &:hover {
      border-color: #409EFF;
    }

    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }

    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
  }
}
</style>